package cn.tedu.tmall.front.mall.service.impl;

import cn.tedu.tamll.common.pojo.CurrentUser;
import cn.tedu.tmall.front.mall.dao.repository.ICartCacheRepository;
import cn.tedu.tmall.front.mall.pojo.po.CartCachePO;
import cn.tedu.tmall.front.mall.pojo.vo.CartVO;
import cn.tedu.tmall.front.mall.service.ICartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Slf4j
@Service
public class CartService implements ICartService {
    @Autowired
    private ICartCacheRepository cartCacheRepository;

    /**
     * 添加商品到购物车
     * @param user
     * @param goodsId
     * @param goodsNumber
     */
    @Override
    public void add(CurrentUser user, Long goodsId, Integer goodsNumber) {
        CartCachePO cartCachePO = new CartCachePO();
        cartCachePO.setGoodsId(goodsId);
        cartCachePO.setGoodsNum(goodsNumber);
        cartCachePO.setGoodsName("iphone160");
        cartCachePO.setAddPrice(1600L);//16*100
        cartCachePO.setChecked(1);
        cartCachePO.setAddCartTime(System.currentTimeMillis());
        //todo 判断是否已存在当前商品

        cartCacheRepository.add(user.getUserId(),cartCachePO);
    }

    @Override
    public List<CartVO> list(Long userId) {
        List<CartCachePO> cartCachePOS = cartCacheRepository.list(userId);
        //时间大的在前面
//        Collections.sort(cartCachePOS, new Comparator<CartCachePO>() {
//            @Override
//            public int compare(CartCachePO o1, CartCachePO o2) {
//                return o2.getAddCartTime().compareTo(o1.getAddCartTime());
//            }
//        });
        Collections.sort(cartCachePOS,Comparator.comparing(CartCachePO::getAddCartTime).reversed());
        List<CartVO> cartVOS = new ArrayList<>();
          cartCachePOS.forEach(po->{
              CartVO cartVO = new CartVO();
              BeanUtils.copyProperties(po,cartVO);
              cartVOS.add(cartVO);
          });

          return cartVOS;
    }

    @Override
    public void modify(Long userId, Long goodsId, Integer goodsNumber) {

        cartCacheRepository.modifyOrReplaceGoodNum(userId,goodsId,goodsNumber,true);
    }

    @Override
    public void incr(Long userId, Long goodsId, Integer goodsAddNumber) {
        cartCacheRepository.modifyOrReplaceGoodNum( userId,  goodsId,  goodsAddNumber,false);
    }

    @Override
    public void reduce(Long userId, Long goodsId, Integer goodsNumber) {
        cartCacheRepository.modifyOrReplaceGoodNum( userId,  goodsId,  -goodsNumber,false);
    }

    @Override
    public void pick(Long userId, Long goodsId) {
        cartCacheRepository.pick(userId,goodsId);
    }

    @Override
    public void pickOff(Long userId, Long goodsId) {
        cartCacheRepository.pickoff(userId,goodsId);
    }

    @Override
    public void delete(Long userId, Long goodsId) {
        //因为做了拆分,需要删除3个key
        cartCacheRepository.deleteByUserIdAndGoodsId(userId,goodsId);
    }

}
